//
//  CollectingViewController.m
//  舒适家
//
//  Created by dllo on 15/10/14.
//  Copyright (c) 2015年 lsc. All rights reserved.
//

#import "CollectingViewController.h"
#import "FirstPageModel.h"
#import "DetailViewController.h"
@interface CollectingViewController ()

@end

@implementation CollectingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title=@"我的收藏";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor blackColor]}];
    
    //导航栏左侧按钮
    UIImage *image = [UIImage imageNamed:@"iconfont-unie642.png"];
    //取消渲染
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftBarAction)];

    self.myTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.myTableView.dataSource=self;
    self.myTableView.delegate=self;
    [self.view addSubview:self.myTableView];
    
    //将数据库中收藏所有的攻略取出来,放入数组
    self.array = [[DataHandle shareDataBaseHandle] selectJingxuan];
    
    //1.使用系统提供的编辑按钮
    self.editButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)leftBarAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier=@"reuse";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    //设置cell点击效果 (没有置灰效果)
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    FirstPageModel *model = [self.array objectAtIndex:indexPath.row];
    cell.textLabel.text = model.title;
    return cell;
}

//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60 * HEIGHTSCALE;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //点击收藏的某个攻略,进入对应的攻略的详情界面
    FirstPageModel *model = [self.array objectAtIndex:indexPath.row];
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    detailVC.firstPageModel = model;//属性传值
    
    //push到下一页(子页面)的时候让tabBar(工具栏)消失
    detailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark -- 编辑删除第2步
//重写系统编辑按钮触发的方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    
    //通过系统编辑按钮控制tableView的编辑状态
    [self.myTableView setEditing:editing animated:animated];
}

#pragma mark -- 编辑删除第3步
//询问哪些行需要编辑(不写这个方法,默认全部都可以编辑)
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

#pragma mark -- 编辑删除第4步
//设置编辑样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleDelete;
    
}

#pragma mark -- 编辑删除第5步
//实现编辑逻辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
   FirstPageModel *model = [self.array objectAtIndex:indexPath.row];
    
    //根据数据库的收藏礼物表中当前标题,删除表中该条信息
    [[DataHandle shareDataBaseHandle] deleteJingxuanTable:model.title];
    
    //数据库的收藏活动表,删除了该活动信息,则数组中也要将该活动也删除
    [self.array removeObjectAtIndex:indexPath.row];
    
    [self.myTableView reloadData];
    
}

//设置删除按钮标题
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
